<?php

namespace app\home\controller;

use app\home\lib\WeiXin;
use think\Db;

class Index extends Base
{
    //首页
    public function index()
    {
        //查询是否签到
        $signLog = Db::name('signin_log')->where(['uid' => $this->user['id'], 'add_time' => date('Y-m-d')])->find();
        $isSignIn = $signLog ? 1 : 0;
        //查询今日和累计赚取余额
        $userEarn = userBalance(config('urls') . '/account/getinfo', $this->user['id']);
        //获取悬赏任务列表
        $taskFirstClassfy = $this->selFirstClassfy();
        //获取广播
        $broadcast = broadcast(config('urls') . '/pay/lasttxlogs', $this->user['id']);
        $broadcastlist = [];
        if ($broadcast['errcode'] == 0) {
            $broadcastdata = json_decode($broadcast['data']['tx']);
            foreach ($broadcastdata as $key => $val) {
                $broadcastlist[$key]['id'] = $val->id;
                $broadcastlist[$key]['payid'] = substr_replace($val->payid, '****', 3, 4);
                $broadcastlist[$key]['integral'] = sprintf("%.2f", ($val->integral / 100));;
            }
        }
        //获取bannner
        $banner = Db::name('config')->where(['type' => 17])->find();
        $bannerList = $banner ? explode(',', $banner['texts']) : [];
        return outputJson(0, 'ok', ['isSignIn' => $isSignIn, 'userEarn' => $userEarn, 'taskList' => $taskFirstClassfy, 'broadcastlist' => $broadcastlist, 'bannerList' => $bannerList]);
    }

    //查询悬赏任务
    private function selFirstClassfy()
    {
        $joblist = joblist(config('xsurls') . '/job/joblist', $this->user['id']);
        $datalists = [];
        foreach ($joblist as $key => $val) {
            $datalists[$key]['id'] = $val['data']['id'];
            $datalists[$key]['title'] = $val['data']['title'];
            $datalists[$key]['subtitle'] = $val['data']['subtitle'];
            $datalists[$key]['img'] = $val['data']['caseImg'];
            $datalists[$key]['unit_price'] = $val['data']['price'];
        }
        return $datalists;
    }

    //签到
    public function signIn()
    {
        $log = Db::name('signin_log')->where(['uid' => $this->user['id']])->find();
        $signNum = ($log && $log['cumulative_num'] < 30) ? ($log['cumulative_num'] + 1) : 1;
        $signin = signLog(config('urls') . '/account/addaccount', $this->user['id'], $signNum);
        if (!$signin) {
            return outputJson(100, 'fail');
        }
        if ($log) {
            if ($log['add_time'] == date('Y-m-d')) {
                return outputJson('100', '今日已签到');
            }
            //成功修改记录
            $result = Db::name('signin_log')->where(['uid' => $this->user['id']])->update(['add_time' => date('Y-m-d')]);
            //当签到累计30时重置计算天数
            if ($log['cumulative_num'] >= 30) {
                $result = Db::name('signin_log')->where(['uid' => $this->user['id']])->update(['cumulative_num' => 1]);
            } else {
                $result = Db::name('signin_log')->where(['uid' => $this->user['id']])->setInc('cumulative_num', 1);
            }
        } else {
            $result = Db::name('signin_log')->insert(['uid' => $this->user['id'], 'add_time' => date('Y-m-d'), 'cumulative_num' => 1]);
        }
        if (!$result) {
            return outputJson(100, 'fail');
        } else {
            //签到成功时  页面展示
            $content = [
                3 => '累计签到3天,额外奖励0.2元',
                7 => '累计签到7天,额外奖励0.4元',
                15 => '累计签到15天,额外奖励0.6元',
                30 => '累计签到30天,额外奖励1.2元',
            ];
            $contents = "";
            $extraContents = "";
            foreach ($content as $k => $val) {
                if ($signNum == $k) {
                    $extraContents = explode(',', $val)[1];
                }
                if ($signNum < $k) {
                    $contents = $val;
                    break;
                }
            }
            if ($signNum == 30) {
                $contents = "恭喜已获得本轮全部奖励";
            }
            return outputJson(0, 'ok', ['dayNum' => $signNum, 'content' => $contents, 'extraContents' => $extraContents]);
        }
    }

    //查询当前进行中的任务
    public function ongoingTasks()
    {
        $getjob = getjob(config('xsurls') . '/job/getjob', $this->user['id'], input('first_id'));
        if ($getjob['errcode'] == 0) {
            $result = json_decode($getjob['data']['data'], true);
            return outputJson(0, 'ok', ['code' => '0', 'id' => $result['id']]);
        }
        if ($getjob['errcode'] == 1) {
            return outputJson(0, $getjob['errmsg'], ['code' => '101', 'id' => $getjob['data']]);
        }
    }

}